package com.rxyl.love.petdoctor.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.rxyl.love.petdoctor.R;

/**
 * Created by Administrator on 2016/1/20 0020.
 */
public class SwitchView extends View {
    private Bitmap switch_bg;
    private Bitmap switch_mask;
    private int bgHeight;
    private int bgWidth;
    private int btnWidth;
    private float currentX;
    private boolean isOn;
    private OnStateChangedListener onStateChangedListener;

    public SwitchView(Context context) {
        super(context);
        initView(context, null);
    }

    public SwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        switch_bg = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.switch_bg);
        switch_mask = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.switch_mask);
        bgHeight = switch_bg.getHeight();
        bgWidth = switch_bg.getWidth();
        btnWidth = switch_mask.getWidth();

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Switch);
        //参数一：要获得的自定义属性在声明的时候的顺序，下标从0开始
        //参数二：如果没有从属性集中得到数据的默认值
        isOn = typedArray.getBoolean(0, true);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(bgWidth, bgHeight);
        setStateOn(isOn);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(switch_bg, 0, 0, null);
        //滑块
        currentX = currentX > btnWidth ? bgWidth / 2 : 0;
        canvas.drawBitmap(switch_mask, currentX, 0, null);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        currentX = event.getX();
        switch (event.getActionMasked()) {
            case MotionEvent.ACTION_UP:
                if (currentX < bgWidth / 2) {
                    isOn = false;
                    currentX = 0;
                } else {
                    isOn = true;
                    currentX = bgWidth;
                }

                if (onStateChangedListener != null) {
                    onStateChangedListener.OnStateChanged(isOn);
                }
                break;
        }
        invalidate();
        return true;
    }


    public void setStateOn(boolean state) {
        if (state) {
            currentX = bgWidth;
            isOn = true;
        } else {
            currentX = 0;
            isOn = false;
        }
        if (onStateChangedListener != null) {
            onStateChangedListener.OnStateChanged(isOn);
        }
        invalidate();
    }

    public void setOnStateChangedListener(OnStateChangedListener onStateChangedListener) {
        this.onStateChangedListener = onStateChangedListener;
    }

    public interface OnStateChangedListener {
        void OnStateChanged(boolean isOn);
    }

}
